/******************************************************************************************
*	Chili DirectX Framework Version 14.03.22											  *
*	Keyboard.cpp																		  *
*	Copyright 2014 PlanetChili.net <http://www.planetchili.net>							  *
*																						  *
*	This file is part of The Chili DirectX Framework.									  *
*																						  *
*	The Chili DirectX Framework is free software: you can redistribute it and/or modify	  *
*	it under the terms of the GNU General Public License as published by				  *
*	the Free Software Foundation, either version 3 of the License, or					  *
*	(at your option) any later version.													  *
*																						  *
*	The Chili DirectX Framework is distributed in the hope that it will be useful,		  *
*	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
*	GNU General Public License for more details.										  *
*																						  *
*	You should have received a copy of the GNU General Public License					  *
*	along with The Chili DirectX Framework.  If not, see <http://www.gnu.org/licenses/>.  *
******************************************************************************************/
#include "Keyboard.h"

KeyboardClient::KeyboardClient(KeyboardServer& kServer)
: server(kServer)
{}

bool KeyboardClient::KeyIsPressed(unsigned char keycode) const
{
	return server.keystates[keycode];
}

KeyEvent KeyboardClient::ReadKey()
{
	if (server.keybuffer.size() > 0)
	{
		KeyEvent e = server.keybuffer.front();
		server.keybuffer.pop();
		return e;
	}
	else
	{
		return KeyEvent(KeyEvent::Invalid, 0);
	}
}

KeyEvent KeyboardClient::PeekKey() const
{
	if (server.keybuffer.size() > 0)
	{
		return server.keybuffer.front();
	}
	else
	{
		return KeyEvent(KeyEvent::Invalid, 0);
	}
}

bool KeyboardClient::KeyEmpty() const
{
	return server.keybuffer.empty();
}

unsigned char KeyboardClient::ReadChar()
{
	if (server.charbuffer.size() > 0)
	{ 
		unsigned char charcode = server.charbuffer.front();
		server.charbuffer.pop();
		return charcode;
	}
	else
	{
		return 0;
	}
}

unsigned char KeyboardClient::PeekChar() const
{
	if (server.charbuffer.size() > 0)
	{
		return server.charbuffer.front();
	}
	else
	{
		return 0;
	}
}

bool KeyboardClient::CharEmpty() const
{
	return server.charbuffer.empty();
}

void KeyboardClient::FlushKeyBuffer()
{
	while (!server.keybuffer.empty())
	{
		server.keybuffer.pop();
	}
}

void KeyboardClient::FlushCharBuffer()
{
	while (!server.charbuffer.empty())
	{
		server.charbuffer.pop();
	}
}

void KeyboardClient::FlushBuffers()
{
	FlushKeyBuffer();
	FlushCharBuffer();
}

KeyboardServer::KeyboardServer()
{
	for (int x = 0; x < nKeys; x++)
	{
		keystates[x] = false;
	}
}

void KeyboardServer::OnKeyPressed(unsigned char keycode)
{
	keystates[keycode] = true;

	keybuffer.push(KeyEvent(KeyEvent::Press, keycode));
	if (keybuffer.size() > bufferSize)
	{
		keybuffer.pop();
	}
}

void KeyboardServer::OnKeyReleased(unsigned char keycode)
{
	keystates[keycode] = false;
	keybuffer.push(KeyEvent(KeyEvent::Release, keycode));
	if (keybuffer.size() > bufferSize)
	{
		keybuffer.pop();
	}
}

void KeyboardServer::OnChar(unsigned char character)
{
	charbuffer.push(character);
	if (charbuffer.size() > bufferSize)
	{
		charbuffer.pop();
	}
}

